load("//private/rules:rules.bzl", "copy_to_workspace", "host_native_binary", "installable_binary")

host_native_binary(
    name = "protoc-gen-go",
    repo = "@com_github_protocolbuffers_protobuf_go_protoc_gen_go",
    target = ":protoc-gen-go",
)

host_native_binary(
    name = "protoc-gen-validate-go",
    repo = "@com_github_bufbuild_protoc_gen_validate",
    target = ":protoc-gen-validate-go",
)

host_native_binary(
    name = "protoc-gen-doc",
    repo = "@com_github_pseudomuto_protoc_gen_doc",
    target = ":protoc-gen-doc",
)

host_native_binary(
    name = "protoc-gen-openapiv2",
    repo = "@com_github_grpc_ecosystem_grpc_gateway_protoc_gen_openapiv2",
    target = "file:protoc-gen-openapiv2",
)

host_native_binary(
    name = "protoc-gen-grpc-gateway",
    repo = "@com_github_grpc_ecosystem_grpc_gateway_protoc_gen_grpc_gateway",
    target = "file:protoc-gen-grpc-gateway",
)

host_native_binary(
    name = "protoc-gen-grpc-gateway-ts",
    repo = "@com_github_grpc_ecosystem_grpc_gateway_protoc_gen_grpc_gateway_ts",
    target = ":protoc-gen-grpc-gateway-ts",
)

host_native_binary(
    name = "gopatch",
    repo = "@org_uber_go_gopatch",
    target = ":gopatch",
)

installable_binary(
    name = "install_gopatch",
    installed_name = "gopatch",
    target = ":gopatch_bin",
)

host_native_binary(
    name = "protoc",
    repo = "@com_github_protocolbuffers_protobuf",
    target = ":bin/protoc",
)

sh_binary(
    name = "compile_sh",
    srcs = ["compile.sh"],
    data = [
        ":fetch_modified.pb.ts",
        ":gopatch_bin",
        ":proto.patch",
        ":protoc-gen-doc_bin",
        ":protoc-gen-go_bin",
        ":protoc-gen-grpc-gateway-ts_bin",
        ":protoc-gen-grpc-gateway_bin",
        ":protoc-gen-openapiv2_bin",
        ":protoc-gen-validate-go_bin",
        ":protoc_bin",
        "//src/proto/pachgen",
        "//src/proto/protoc-gen-zap",
        "//src/proto/prototar",
        "@com_github_chrusty_protoc_gen_jsonschema//cmd/protoc-gen-jsonschema",
        "@com_google_protobuf//:descriptor_proto_srcs",
        "@com_google_protobuf//:well_known_type_protos",
        "@go_sdk//:bin/gofmt",
        "@org_golang_google_grpc_cmd_protoc_gen_go_grpc//:protoc-gen-go-grpc",
    ],
    deps = ["@bazel_tools//tools/bash/runfiles"],
)

genrule(
    name = "gen_go_proto_bundle",
    outs = [
        "go_protos.tar",
        "go_protos_forgotten_files.txt",
    ],
    cmd = "$(location compile_sh) $(location go_protos.tar) $(location go_protos_forgotten_files.txt) $(locations //src:all_protos)",
    tools = [
        "compile_sh",
        "//src:all_protos",
    ],
)

# bazel build //src/proto:run will regenerate the protos in your working copy.
copy_to_workspace(
    name = "run",
    src = "go_protos.tar",
    message = "Go protos are outdated.  Run 'bazel run //:make_proto' to update them.",
    visibility = ["//visibility:public"],
)
